package com.longrise.android.byjk.plugins.hra.hratabsecond;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseFragment;
import com.longrise.common.utils.PrintLog;

import java.util.List;

/**
 * Created by Administrator on 2018/1/22.
 */

public class OrderSearchDetailFragment extends BaseFragment<MineAppointPresenter> implements MineAppointContract.View, MineAppointDetailAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {

    public static final String ORDERSEARCHSTATE = "ordersearchstate";
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    private String orderState;  //待检测,已检测
    private MineAppointDetailAdapter mAdapter;
    private View mHeadView;
    private List<MineAppointDetailBean> mListBeans;
    private String orderType = "";
    private EntityBean[] mHraOrderListBean;
    private LinearLayout ll_appoint_null;

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_order_search_detail;
    }

    @Override
    public void initView() {
        mSrl = (BBswipeRefreshLayout) mRootView.findViewById(R.id.mine_appoint_search_sp);
        mRcv = (RecyclerView) mRootView.findViewById(R.id.mine_appoint_search_rv);
        ll_appoint_null = (LinearLayout) mRootView.findViewById(R.id.layout_appoint_search_null);
        Bundle arguments = getArguments();
        orderState = arguments.getString(ORDERSEARCHSTATE);
        initRcv();
        initEvent();
        mPresenter.getOrderSearchData(getOrderState(orderState), Control.getOrderSearchName());
    }

    public String getOrderState(String orderState){
        if("待检测".equals(orderState)){
            orderType = "0";
        }else if ("已检测".equals(orderState)){
            orderType = "1";
        }
        return orderType;
    }


    private void initRcv() {
        mSrl.setOnRefreshListener(this);
        mHeadView = View.inflate(mContext, R.layout.item_mine_appoint_headerview, null);
        mAdapter = new MineAppointDetailAdapter();
        mAdapter.addHeaderView(mHeadView);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
    }

    public void initEvent(){
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    //跳转到预约详情界面
    @Override
    public void onItemClick(EntityBean item) {
        String orderid = item.getString("orderid");
        Intent intent = new Intent(mContext,MineAppointDetailActivity.class);
        intent.putExtra(MineAppointDetailActivity.MINEORDERID,orderid);
        startActivity(intent);
    }

    @Override
    public void refreshData(EntityBean bean) {
        mHraOrderListBean = bean.getBeans("result");
        PrintLog.e(TAG,"mHraOrderListBean=:"+mHraOrderListBean);
        if(mHraOrderListBean != null){
            mAdapter.refreshData(mHraOrderListBean);
            mSrl.setVisibility(View.VISIBLE);
            ll_appoint_null.setVisibility(View.GONE);
        }else {
            ll_appoint_null.setVisibility(View.VISIBLE);
            mSrl.setVisibility(View.GONE);
        }


    }

    @Override
    public void setRefreshing(boolean isRefreshing) {
        mSrl.setRefreshing(isRefreshing);
    }

    @Override
    public void onRefresh() {
        mPresenter.getOrderSearchData(getOrderState(orderState), Control.getOrderSearchName());
    }
}
